﻿/*
 * zunit_unittest.cxx
 *
 *  Created on: 2015年4月10日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zalgorithm/zunit.h"

using namespace zpsoe::zalgorithm;

TEST(ZunitTest, to_str) {
    EXPECT_STREQ("byte", zunit::to_str(SIZE_Byte).c_str());
    EXPECT_STREQ("KiB", zunit::to_str(SIZE_KiB).c_str());
    EXPECT_STREQ("MiB", zunit::to_str(SIZE_MiB).c_str());
    EXPECT_STREQ("GiB", zunit::to_str(SIZE_GiB).c_str());
    EXPECT_STREQ("TiB", zunit::to_str(SIZE_TiB).c_str());
    EXPECT_STREQ("PiB", zunit::to_str(SIZE_PiB).c_str());
    EXPECT_STREQ("KB", zunit::to_str(SIZE_KB).c_str());
    EXPECT_STREQ("MB", zunit::to_str(SIZE_MB).c_str());
    EXPECT_STREQ("GB", zunit::to_str(SIZE_GB).c_str());
    EXPECT_STREQ("TB", zunit::to_str(SIZE_TB).c_str());
    EXPECT_STREQ("PB", zunit::to_str(SIZE_PB).c_str());
}

TEST(ZunitTest, to_byte1) {
    EXPECT_EQ(1L, zunit::to_byte(SIZE_Byte));

    EXPECT_EQ(1024L, zunit::to_byte(SIZE_KiB));
    EXPECT_EQ(1048576L, zunit::to_byte(SIZE_MiB));
    EXPECT_EQ(1073741824L, zunit::to_byte(SIZE_GiB));
    EXPECT_EQ(1099511627776L, zunit::to_byte(SIZE_TiB));
    EXPECT_EQ(1125899906842624L, zunit::to_byte(SIZE_PiB));

    EXPECT_EQ(1000L, zunit::to_byte(SIZE_KB));
    EXPECT_EQ(1000000L, zunit::to_byte(SIZE_MB));
    EXPECT_EQ(1000000000L, zunit::to_byte(SIZE_GB));
    EXPECT_EQ(1000000000000L, zunit::to_byte(SIZE_TB));
    EXPECT_EQ(1000000000000000L, zunit::to_byte(SIZE_PB));
}

TEST(ZunitTest, to_byte2) {
    const ssize_t s_ = 3;

    EXPECT_EQ(3, zunit::to_byte(SIZE_Byte, s_));
}

TEST(ZunitTest, to_size) {
    // 1024 PiB
    const byte_t b1_ = 1152921504606846976L;

    EXPECT_DOUBLE_EQ(1152921504606846976, zunit::to_size(SIZE_Byte, b1_, 0));

    EXPECT_DOUBLE_EQ(1125899906842624.0, zunit::to_size(SIZE_KiB, b1_, 0));
    EXPECT_DOUBLE_EQ(1099511627776.0, zunit::to_size(SIZE_MiB, b1_, 0));
    EXPECT_DOUBLE_EQ(1073741824.0, zunit::to_size(SIZE_GiB, b1_, 0));
    EXPECT_DOUBLE_EQ(1048576.0, zunit::to_size(SIZE_TiB, b1_, 0));
    EXPECT_DOUBLE_EQ(1024.0, zunit::to_size(SIZE_PiB, b1_, 0));

    EXPECT_DOUBLE_EQ(1152921504606847.00, zunit::to_size(SIZE_KB, b1_, 2));
    EXPECT_DOUBLE_EQ(1152921504606.85, zunit::to_size(SIZE_MB, b1_, 2));
    EXPECT_DOUBLE_EQ(1152921504.61, zunit::to_size(SIZE_GB, b1_, 2));
    EXPECT_DOUBLE_EQ(1152921.50, zunit::to_size(SIZE_TB, b1_, 2));
    EXPECT_DOUBLE_EQ(1152.92, zunit::to_size(SIZE_PB, b1_, 2));

    // 1000 PB
    const byte_t b2_ = 1000000000000000000L;

    EXPECT_DOUBLE_EQ(1000000000000000000, zunit::to_size(SIZE_Byte, b2_, 0));

    EXPECT_DOUBLE_EQ(976562500000000.00, zunit::to_size(SIZE_KiB, b2_, 2));
    EXPECT_DOUBLE_EQ(953674316406.25, zunit::to_size(SIZE_MiB, b2_, 2));
    EXPECT_DOUBLE_EQ(931322574.62, zunit::to_size(SIZE_GiB, b2_, 2));
    EXPECT_DOUBLE_EQ(909494.70, zunit::to_size(SIZE_TiB, b2_, 2));
    EXPECT_DOUBLE_EQ(888.18, zunit::to_size(SIZE_PiB, b2_, 2));

    EXPECT_DOUBLE_EQ(1000000000000000.0, zunit::to_size(SIZE_KB, b2_, 0));
    EXPECT_DOUBLE_EQ(1000000000000.0, zunit::to_size(SIZE_MB, b2_, 0));
    EXPECT_DOUBLE_EQ(1000000000.0, zunit::to_size(SIZE_GB, b2_, 0));
    EXPECT_DOUBLE_EQ(1000000.0, zunit::to_size(SIZE_TB, b2_, 0));
    EXPECT_DOUBLE_EQ(1000.0, zunit::to_size(SIZE_PB, b2_, 0));
}
